/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package movil;

import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.midlet.*;

/**
 * @author Windows XP
 */
public class HashTableExample extends MIDlet implements CommandListener, PlayerListener{

    private Display display;
    private List itemList;
    private Form form;
    private Command stop, start, pause;
    private Hashtable items, itemsInfo;
    private Player player;

    public HashTableExample(){
        display = Display.getDisplay(this);
        itemList = new List("Select an item to play", List.IMPLICIT);
        stop = new Command("Stop", Command.STOP, 1);
        pause = new Command("Pause", Command.ITEM, 1);
        start = new Command("Star", Command.ITEM, 1);
        form = new Form("Playing media");
        form.addCommand(stop);
        form.addCommand(pause);
        form.setCommandListener(this);

        /* Implementing Hashtable */
        items = new Hashtable();
        itemsInfo = new Hashtable();

        items.put("Police siren", "file://siren.wav");
        itemsInfo.put("Police siren", "audio/x-wav");

        items.put("Packman sound", "file://PacMan.wav");
        itemsInfo.put("Packman sound", "audio/x-wav");
    }

    public void startApp() {
        for(Enumeration en = items.keys(); en.hasMoreElements();){
            itemList.append((String)en.nextElement(), null);
        }
        itemList.setCommandListener(this);
        display.setCurrent(itemList);
    }

    public void pauseApp() {
        try{
            if(player != null) player.stop();
        }catch(Exception e){

        }
    }

    public void destroyApp(boolean unconditional) {
        if(player != null) player.close();
    }

    public void commandAction(Command c, Displayable d) {
        if(d instanceof List){
            List list = ((List)d);
            String key = list.getString(list.getSelectedIndex());
            try{
                playMedia((String)items.get(key), key);
            }catch(Exception e){
                System.err.println("Unable to play: "+ e);
                e.printStackTrace();
            }
        }else if(d instanceof Form){
            try{
                if(c == stop){
                    player.close();
                    display.setCurrent(itemList);
                    form.removeCommand(start);
                    form.addCommand(pause);
                }else if(c == pause){
                    player.stop();
                    form.removeCommand(pause);
                    form.addCommand(start);
                }else if(c == start){
                    player.start();
                    form.removeCommand(start);
                    form.addCommand(pause);
                }
            }catch(Exception e){
                System.err.println(e);
            }
        }
    }

    public void playerUpdate(Player player, String event, Object eventData) {
        if(event.equals(PlayerListener.STARTED) && new Long(0L).equals((Long)eventData)){
            display.setCurrent(form);
        }else if(event.equals(PlayerListener.CLOSED)){
            form.deleteAll();
        }
    }

    private void playMedia(String locator, String key) throws Exception{
        String file = locator.substring(locator.indexOf("file://") + 6,
                locator.length());
        player = Manager.createPlayer(getClass().getResourceAsStream(file),
                (String)itemsInfo.get(key));
        player.addPlayerListener(this);
        player.setLoopCount(-1);
        player.prefetch();
        player.realize();
        player.start();
    }
}
